﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VNPT.Data
{
   public static class DataConnect
    {
        public static DbNetData DbServer { get; set; }

        
    }
   public static class DoiSoat_SQL
   {
       public static string GetValue(string section, string entry, string defaultValue="")
       {
           if (Data.DataConnect.DbServer.Conn.State == System.Data.ConnectionState.Open)
           {
               var cm = new Data.CommandConfig("SELECT GIATRI FROM DoiSoat_SQL Where NHOM=:section and KHOA=:entry And rownum = 1");
               cm.Params.Add("section", section);
               cm.Params.Add("entry", entry);

               var value = (Data.DataConnect.DbServer.ExecuteScalar(cm) ?? defaultValue).ToString();
               return value;
           }
           new Exception("Không có kết nối đến server");
           return "";
       }
       public static void SetValue(string section, string entry, string value)
       {
           if (Data.DataConnect.DbServer.Conn.State == System.Data.ConnectionState.Open)
           {
               var cm = new Data.CommandConfig("SELECT count(*) As ABC FROM DoiSoat_SQL Where NHOM=:section and KHOA=:entry And rownum = 1");
               cm.Params.Add("section", section);
               cm.Params.Add("entry", entry);

               var check = (Data.DataConnect.DbServer.ExecuteScalar(cm) ?? "0").ToString();

              //var check =(Data.DataConnect.DbServer.ExecuteScalar(
              //     new Data.CommandConfig(string.Format("SELECT count(*) As ABC FROM DoiSoat_SQL Where NHOM='{0}' and KHOA='{1}'", section, entry)))
              //                             ?? "0").ToString();

              int dem = 0;
              int.TryParse(check, out dem);
              if (dem>=1)
              {
                  var cm2 = new Data.CommandConfig("UPDATE DoiSoat_SQL SET GiaTri=:value Where NHOM=:section and KHOA=:entry");
                  cm2.Params.Add("section", section);
                  cm2.Params.Add("entry", entry);
                  cm2.Params.Add("value", value);
                  Data.DataConnect.DbServer.ExecuteNonQuery(cm2);

              }
              else
              {

                  var cm2 = new Data.CommandConfig("INSERT INTO DoiSoat_SQL(NHOM, KHOA, GIATRI) VALUES (:section, :entry, :value) ");
                  cm2.Params.Add("section", section);
                  cm2.Params.Add("entry", entry);
                  cm2.Params.Add("value", value);
                  Data.DataConnect.DbServer.ExecuteNonQuery(cm2);

              }
               
           }
           new Exception("Không có kết nối đến server");
       }
   }
}
